package com.im.app.controller;

import com.im.app.model.Welcome;
import com.im.app.service.WelcomeService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/welcome")
public class WelcomeController {

    @Resource
    private WelcomeService welcomeService;

    @RequestMapping(value = "/queryWelcomeList",method=RequestMethod.POST)
    public ResponseResult<List<Welcome>> queryWelcomeList() {
        try {
            //调用业务层返回列表
            List<Welcome> welcomeList = welcomeService.queryWelcomeList();
            return Response.makeOKRsp(welcomeList);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询欢迎语信息异常");
        }
    }

    @RequestMapping(value ="/find/{id}",method=RequestMethod.POST)
    public ResponseResult<Welcome> findWelcome(@PathVariable("id") Integer id) {
        try {
            //调用业务层返回列表
            Welcome welcome = welcomeService.findWelcome(id);
            return Response.makeOKRsp(welcome);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询欢迎语信息异常");
        }
    }

    @RequestMapping(value ="/delete/{id}",method=RequestMethod.POST)
    public ResponseResult deleteWelcome(@PathVariable("id") Integer id) {
        int result = welcomeService.deleteWelcome(id);
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }

    }

    @RequestMapping(value ="/add",method=RequestMethod.POST)
    public ResponseResult addWelcome(Welcome welcome) {
        int result = welcomeService.addWelcome(welcome);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @RequestMapping(value ="/update",method=RequestMethod.POST)
    public ResponseResult updateWelcome(Welcome welcome) {

        int result = welcomeService.updateWelcome(welcome);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }

    }



}
